﻿using Microsoft.Xna.Framework;
using MoreEvent.Common.Players;
using Terraria;
using Terraria.ID;
using Terraria.ModLoader;

namespace MoreEvent.Content.Projectiles
{
    public class FrenchFrie : ModProjectile
    {

        private float rotationAngle = 0f;
        private int index = 0;
        private int totalCount = 8;
        public override void SetStaticDefaults()
        {
            ProjectileID.Sets.TrailCacheLength[Projectile.type] = 9;
            ProjectileID.Sets.TrailingMode[Projectile.type] = 0;
            ProjectileID.Sets.MinionSacrificable[Projectile.type] = true;
            ProjectileID.Sets.MinionTargettingFeature[Projectile.type] = true;
        }

        public override void SetDefaults()
        {
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCHitCooldown = 60;
            Projectile.width = 14;
            Projectile.height = 14;
            Projectile.friendly = true;
            Projectile.DamageType = DamageClass.Default;
            Projectile.penetrate = -1;
            Projectile.timeLeft = 3600;
            Projectile.tileCollide = false;
            Projectile.alpha = 80;
            Projectile.minion = true;
            Projectile.minionSlots = 0f;
        }

        public override void AI()
        {
            Player player = Main.player[Projectile.owner];
            if (player.dead || !player.active || !PlayerHasAccessory(player))
            {
                Projectile.Kill();
                return;
            }
            if (index == 0)
            {
                for (int i = 0; i < totalCount; i++)
                {
                    bool indexTaken = false;
                    for (int j = 0; j < Main.maxProjectiles; j++)
                    {
                        if (Main.projectile[j].active && Main.projectile[j].type == Projectile.type && Main.projectile[j].owner == Projectile.owner && (Main.projectile[j].ModProjectile as FrenchFrie)?.index == i)
                        {
                            indexTaken = true;
                            break;
                        }
                    }
                    if (!indexTaken)
                    {
                        index = i;
                        break;
                    }
                }
            }
            rotationAngle += 0.04f;
            float radius = 160f;
            float angle = MathHelper.TwoPi * index / totalCount + rotationAngle;
            Vector2 targetPos = player.Center + new Vector2(radius, 0).RotatedBy(angle);
            Projectile.Center = Vector2.Lerp(Projectile.Center, targetPos, 0.3f);
            Projectile.rotation = angle + MathHelper.PiOver2;
            Lighting.AddLight(Projectile.Center, new Vector3(1f, 0.8f, 0.2f) * 0.3f);
            if (Main.rand.NextBool(6))
            {
                Dust dust = Dust.NewDustDirect(Projectile.position, Projectile.width, Projectile.height, DustID.GoldFlame, 0f, 0f, 100, default, 0.8f);
                dust.noGravity = true;
                dust.velocity = Main.rand.NextVector2Circular(0.5f, 0.5f);
            }
        }

        private static bool PlayerHasAccessory(Player player)
        {
            if (player.GetModPlayer<AccPlayer>().LegendaryFamilyMeal)
            {
                return true;
            }
            return false;
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            for (int i = 0; i < 2; i++)
            {
                Dust dust = Dust.NewDustDirect(Projectile.position, Projectile.width, Projectile.height, DustID.YellowTorch, 0f, 0f, 100, default, 0.8f);
                dust.noGravity = true;
                dust.velocity = Main.rand.NextVector2Circular(1.5f, 1.5f);
            }
        }

        public override bool? CanCutTiles()
        {
            return false;
        }

        public override bool MinionContactDamage()
        {
            return true;
        }
    }
}
